// cardCollectionPack/pages/cardCollection/cardsDisplay/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    barMarginTop: '16',
    barHeight: '52',
    barWidth: '0',
    scrollHeight: '',
    // isOverShare: true,
    activeTitleImg: '',
    allCollectedNum: 102400,
    openTime: '11月11日11点',
    cardCollectionData: {},
    cardsList: [], // 卡片列表
    currentBigCard: 0, // 当前大卡
    currentCardId: 0, // 当前mini卡
    currentCardInfo: {}, // 当前卡片信息
    isAllCollect: false, // 是否已集齐九牛
    isMerge: false, // 是否已合成仨牛
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBarHeight()
    this.setListHeight()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (opt) {
    let _this = this
    let curInfo = _this.data.currentCardInfo
    let title = '祝您' + curInfo.name
    let img = curInfo.bigCardUrl
    let data = {
      cardShare_id: curInfo.id
    }
    let url = app.getShareUrlParams(data)
    if (opt.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: title,
        imageUrl: img,
        path: "/pages/index/index?" + url
      }
    }
  },

  /**
   * 设置导航栏高度
   */
  getBarHeight() {
    let systemInfo = wx.getSystemInfoSync()
    let	statusBarHeight = systemInfo.statusBarHeight
    let menu = wx.getMenuButtonBoundingClientRect()

    let	barMarginTop = menu.top
    let	barHeight = menu.height + 10
    let barWidth = systemInfo.windowWidth - (systemInfo.windowWidth - menu.right + menu.width)

    this.setData({
      barMarginTop: barMarginTop,
      barHeight: barHeight,
      barWidth: barWidth
    })
  },

  /**
   * 设置scroll-view高度
   */
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      hei = parseInt(this.data.barMarginTop) + parseInt(this.data.barHeight),
      tapHeight = Math.floor(rpx * hei * 2), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
    this.setData({
      scrollHeight
    });
  },

  getData() {
    let cardData = app.getCardCollectionData()
    let cardsList = JSON.parse(JSON.stringify(app.getCardsList()))
    if(!cardData.isAllCollect && !cardData.isMerge) {
      // 未集齐, 未合成
      cardsList.splice(0, 1)
    } else if(cardData.isAllCollect && !cardData.isMerge) {
      // 已集齐, 未合成
    } else if(!cardData.isAllCollect && cardData.isMerge) {
      // 未集齐, 已合成
    }
    this.setData({
      cardsList: cardsList,
      cardCollectionData: cardData,
      isAllCollect: cardData.isAllCollect, // 是否已集齐九牛
      isMerge: cardData.isMerge, // 是否已合成仨牛
      currentCardId: cardsList[0].id, // 当前mini卡
      currentCardInfo: cardsList[0], // 当前卡片信息
    })
  },

  bigCardChange(e) {
    let curInfo = this.data.cardsList[e.detail.current]
    this.setData({
      currentBigCard: e.detail.current,
      currentCardId: curInfo.id,
      currentCardInfo: curInfo,
    })
  },
  selectCard(e) {
    let curId = e.currentTarget.dataset.id
    let curInfo = e.currentTarget.dataset.info
    let currentBigCard = this.data.cardsList.findIndex((val) => val.id === curId)
    this.setData({
      currentBigCard: currentBigCard,
      currentCardId: curId,
      currentCardInfo: curInfo,
    })
  },
  goIndex() {
    wx.navigateBack({
      fail: function() {
        wx.redirectTo({
          url: '/cardCollectionPack/pages/cardCollection/index',
        })
      }
    })
  },
  goPrizeDetails() {
    wx.navigateTo({
      url: '/cardCollectionPack/pages/cardCollection/prizeDetails/index',
    })
  },
})